<?php
/**
 * decode explorer controller
 * @author Erko Risthein <erko@risthein.ee>
 * @copyright Copyright (C) 2009 Erko Risthein
 * @link http://erko.risthein.ee/decode/
 * @license http://www.gnu.org/licenses/ GNU General Public License
 * @package decode_explorer
 */

function __autoload($class_name) {
	require_once($class_name . '.php');
}
require('Fallback.php');

class DecodeExplorer {
	private $path;
	private $sort_by;
	private $sort_in;
	//default config
	public static $rootpath = '.';
	public static $page_title = 'decode';
	public static $language = 'en';
	
	/**
	 * Parses the configuration array, parses the GET variables, creates the DirList object, builds the data array and injects the data to the view
	 * @param array $config
	 */
	function __construct($config) {
		//parse the config array and the GET variables
		$this->parse_config($config);
		$this->parse_get_variables();
		
		//request the directory listing
		$dir_list = new DirList($this->path, $this->sort_by, $this->sort_in);
		
		//build the data array
		$data = array();
		$data['language'] = self::$language;
		$data['breadcrumb'] = $dir_list->get_breadcrumb();
		$data['sorted_by'] = $dir_list->get_sorted_by();
		$data['sorted_in'] = $dir_list->get_sorted_in();
		$data['name_sort_url'] = $dir_list->get_name_sort_url();
		$data['size_sort_url'] = $dir_list->get_size_sort_url();
		$data['date_sort_url'] = $dir_list->get_date_sort_url();
		$data['folders'] = $dir_list->get_folders();
		$data['files'] = $dir_list->get_files();
		$data['title'] = self::$page_title;
		$data['parent_folder'] = $dir_list->get_parent_folder();
		$data['total_objects'] = $dir_list->get_total_objects();
		$data['total_size'] = $dir_list->get_total_size();
		
		//inject it to the view
		$view = isset($_GET['json']) ? 'JSON' : 'DecodeExplorer';
		View::render($view . 'View.php', $data);
	}
	
	/**
	 * Parses the configuration array
	 * @param array $config
	 */
	private function parse_config($config) {
		self::$rootpath = $config['rootpath'];
		self::$page_title = $config['page_title'];
		self::$language = $config['language'];
	}
	
	/**
	 * Parses the GET variables
	 * @param array $config
	 */
	private function parse_get_variables() {
		//path
		if (isset($_GET['path'])) {
			$this->path = $this->validate_path($_GET['path']);
		} else {
			$this->path = self::$rootpath;
		}
		//sort by
		if (isset($_GET['sort_by']) && ($_GET['sort_by'] == 'size' || $_GET['sort_by'] == 'date')) {
			$this->sort_by = $_GET['sort_by'];
		} else {
			$this->sort_by = 'name';
		}
		//sort in
		if (isset($_GET['sort_in']) && ($_GET['sort_in'] == 'desc')) {
			$this->sort_in = 'desc';
		} else {
			$this->sort_in = 'asc';
		}
	}
	
	/**
	 * Makes sure we are not trying to go down from the rootdir
	 * @param string $path to validate
	 * @return string validated path
	 */
	private function validate_path($path) {
		$path = Helper::add_rootpath($path); //the rootpath was previously removed
		$realpath = realpath($path);
		$rootpath = realpath(self::$rootpath);
		if (!empty($path) && substr_compare($rootpath, $realpath, 0, strlen($rootpath)) == 0) { //if it's a valid path eg. we're not trying to go down from the rootdir
			return $path;
		}
		return self::$rootpath; //it wasn't a valid path eg. we tried to go down from the rootdir
	}
}
?>